package com.dabai.linktools.activitys;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.dabai.linktools.MainActivity;
import com.dabai.linktools.R;
import com.dabai.linktools.databinding.ActivityLinkCollectionBinding;
import com.dabai.linktools.utils.DabaiUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LinkCollectionActivity extends AppCompatActivity {

    //    这三个经常是同时出现的
    private List<Map<String, String>> lists;
    private SimpleAdapter adapter;
    private ListView listView;

    ActivityLinkCollectionBinding alcb;
    private String TAG = "dabaizzz";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        alcb = ActivityLinkCollectionBinding.inflate(LayoutInflater.from(this));
        setContentView(alcb.getRoot());

        //准备数据源
        lists = new ArrayList<>();

        SharedPreferences sp = this.getSharedPreferences("Collection", 0);
        Map<String, String> map = (Map<String, String>) sp.getAll();


        for (String key : map.keySet()) {

            String value = map.get(key);

            Map<String, String> tmpmap = new HashMap<>();
            tmpmap.put("key", key);
            tmpmap.put("value", value);

            lists.add(tmpmap);

        }

        adapter = new SimpleAdapter(this, lists, R.layout.collection_item
                , new String[]{"key", "value"}
                , new int[]{R.id.textView1, R.id.textView2});
        alcb.lv.setAdapter(adapter);

        alcb.lv.setOnItemLongClickListener((parent, view, position, id) -> {
            String value = lists.get(position).get("value");

            ClipboardManager clipboardManager = (ClipboardManager) getApplicationContext().getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData mclipData = ClipData.newPlainText("Label", value);
            clipboardManager.setPrimaryClip(mclipData);

            Toast.makeText(LinkCollectionActivity.this, "复制内容成功!", Toast.LENGTH_SHORT).show();

            return true;
        });

    }

    //菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.collection, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.clear:
                SharedPreferences sp = this.getSharedPreferences("Collection", 0);
                SharedPreferences.Editor editor = sp.edit();
                editor.clear();
                if (editor.commit()){
                    Toast.makeText(this, "清空成功!", Toast.LENGTH_SHORT).show();
                    finish();
                }else {
                    Toast.makeText(this, "清空失败!", Toast.LENGTH_SHORT).show();
                }
                break;

        }
        return super.onOptionsItemSelected(item);
    }

}
